<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BTreeItem
 *
 * @author jianfeng
 */
class BTreeItem extends BTreeEntity {

    //put your code here
    public $name;
    public $value;
    public $owner;
    public $ownerType;
    public $ownerId;
    public $valueType;
    public $itemType;
    public $groupCount;
    public $spaceId;
    public $appId;
    public $data;

    public function __construct($item = NULL, $owner = NULL) {
        if ($owner instanceof BEntity) {
            $this->owner = $owner;
            $this->ownerType = get_class($owner);
            if ($owner->getId() != NULL)
                $this->ownerId = $owner->getId();
        }

        //if ($this->name != 'root') {
        //$this->itemType = 'BTreeItem';
        //}
        parent::__construct($item);
    }

    public function getName($data = NULL) {
        return $this->name;
    }

    public function getValue($data = NULL) {
        if ($this->valueType == 'string' || $this->valueType == NULL) {
            return $this->value;
        }
        if ($this->valueType == 'non-string') {
            $data = $this->data;
            return unserialize($data);
        }
    }

    public function getRootNode($context = NULL) {
        $root = self::getRoot();

        $myContext = $this->getRootNodeContext($context);

        $myRoot = $root->getChild($myContext);

        if ($myRoot instanceof BTreeEntity && $myRoot->getId() != NULL) {
            return $myRoot;
        } else {
            $myRoot = $root->addChild($myContext);
            return $myRoot;
        }
    }

    public function getRootNodeContext($context = NULL) {
        $myContext = array(
            'name' => 'tree_root');


        //'spaceId' => Blyn::app()->getActiveSpace()->getId(),
        //'appId' => Blyn::app()->getActiveApp()->getId(),
        //'ownerType' => $this->ownerType,
        //'ownerId' => $this->ownerId,
        //'myClass' => get_class($this)
        //if(Blyn::app()->getActiveSpace() instanceof BSpace){
        //$myContext['spaceId'] = Blyn::app()->getActiveSpace()->getId();
        //}

        if ($this->ownerType != NULL) {
            $myContext['ownerType'] = $this->ownerType;
        }

        if ($this->ownerId != NULL) {
            $myContext['ownerId'] = $this->ownerId;
        }

        if (is_array($context)) {
            $myContext += $context;
        }

        if ($this->spaceId != NULL) {
            $myContext['spaceId'] = $this->spaceId;
        }

        if ($this->appId != NULL) {
            $myContext['appId'] = $this->appId;
        }

        return $myContext;
    }

    public function getItemClass($data = NULL) {
        return get_class($this);
    }

    public function getRepository($data = NULL, $reload = FALSE) {
        $repository = parent::getRepository($data, $reload);
        $repository->addPropertyToColumnMap('ownerType', 'owner_type');
        $repository->addPropertyToColumnMap('ownerId', 'owner_id');
        $repository->addPropertyToColumnMap('itemType', 'item_type');
        $repository->addPropertyToColumnMap('userId', 'user_id');
        $repository->addPropertyToColumnMap('groupCount', 'count');
        $repository->addPropertyToColumnMap('valueType', 'value_type');
        $repository->addPropertyToColumnMap('data', 'data');


        $repository->addSearchablePropertyList('ownerType');
        $repository->addSearchablePropertyList('ownerId');
        $repository->addSearchablePropertyList('spaceId');
        $repository->addSearchablePropertyList('appId');
        $repository->addSearchablePropertyList('userId');
        $repository->addSearchablePropertyList('itemType');
        return $repository;
    }

    public function setOwner($owner) {
        if ($owner instanceof BEntity) {
            $this->owner = $owner;
            $this->ownerType = get_class($owner);
            if ($owner->getId() != NULL)
                $this->ownerId = $owner->getId();
        }
    }

    public function getPropertyToColumnMapList($data = NULL) {
        $list = parent::getPropertyToColumnMapList($data);
        $list['itemType'] = 'item_type';
        $list['ownerType'] = 'owner_type';
        $list['ownerId'] = 'owner_id';
        $list['groupCount'] = 'count';
        $list['valueType'] = 'value_type';
        $list['data'] = 'data';
        return $list;
    }

    public function save($context = null) {
        if ($this->value != NULL) {
            if ((is_string($this->value) && strlen($this->value) < 256) || is_numeric($this->value)) {
                $this->valueType = 'string';
            } else {
                $this->data = serialize($this->value);
                $this->valueType = 'non-string';
                $this->value = NULL;
            }
        }
        if ($this->itemType == NULL)
            $this->itemType = 'TreeItem';
        parent::save($context);
    }

}
